const daoCommon = require('../util/daoCommon');

const dao = {
    handleGetAppointment: (data) => {
        let sql;
        let page = (data.page - 1) * Number(data.size);
        data.page = page;
        sql = `select a.*,s.service_name from appointment a inner join service s on a.service_id=s.service_id and a.is_delete='0' limit ${data.page},${data.size}`;
        return daoCommon.selectCommon(sql);
    },
    handleSearchAppoint: (data) => {
        let page = (data.page - 1) * Number(data.size);
        data.page = page;
        let sql = "select * from appointment where 1=1";
        if (data.people) {
            sql += " and appointment_people like concat('%', $people, '%')";
        }
        if (data.phone) {
            sql += " and appointment_phone like concat('%', $phone, '%')";
        }
        if (data.startTime && data.endTime) {
            sql += " and appointment_time BETWEEN $startTime AND $endTime";
        }
        if (data.serviceName) {
            sql += " and service_name=$serviceName";
        }
        if (data.state) {
            sql += " and appointment_state=$state";
        }
        sql += ` and is_delete='0' limit ${data.page},${data.size}`;
        return daoCommon.selectCommon(sql, data);
    },
    handleGetAllSize: () => {
        let sql;
        sql = "select count(*) as count from appointment where is_delete='0'";
        return daoCommon.getSize(sql);
    },
    handleGetSearchSize: (data) => {
        let sql = 'select count(*) as count from appointment where 1=1';
        if (data.people) {
            sql += " and appointment_people like concat('%', $people, '%')";
        }
        if (data.phone) {
            sql += " and appointment_phone like concat('%', $phone, '%')";
        }
        if (data.startTime && data.endTime) {
            sql += " and appointment_time BETWEEN $startTime AND $endTime";
        }
        if (data.serviceName) {
            sql += " and service_name=$serviceName";
        }
        if (data.state) {
            sql += " and appointment_state=$state";
        }
        sql += " and is_delete='0'";
        return daoCommon.getSize(sql, data);
    },
    handleDeleteAppointment: (data) => {
        let sql;
        sql = "update appointment set is_delete='1' where appointment_id=$id";
        return daoCommon.queryCommon(sql, data);
    },
    handleEditAppointment: (data) => {
        let sql;
        const date = new Date();
        data.updatedTime = date.toLocaleString();
        sql = "update appointment set appointment_remark=$remark,updated_time=$updatedTime,appointment_state='1' where appointment_id=$id";
        return daoCommon.queryCommon(sql, data);
    }
};
module.exports = dao;